package com.wiegand.util {
	import flash.display.Sprite;

	/**
	 * @author gandy
	 */
	public class Cube extends Sprite {
		private var _front : Sprite;
		private var _back : Sprite;
		private var _top : Sprite;
		private var _bottom : Sprite;
		private var _left : Sprite;
		private var _right : Sprite;
		private var _materials : MaterialsList;
		private var _startRot : Array;
		public function  Cube( materials:MaterialsList, cwidth:int=100, cdepth:int=100, cheight:int=100){
			
			_materials = materials;
			
			_startRot = new Array();
			
			_front = materials.getMaterialByName("front");
			_back = materials.getMaterialByName("back");
			_top = materials.getMaterialByName("top");
			_bottom = materials.getMaterialByName("bottom");
			_left = materials.getMaterialByName("left");
			_right = materials.getMaterialByName("right");
			
			_front.x = - Math.round(cwidth/2);
			_front.y = - Math.round(cheight/2);
			_front.z = + Math.round(cdepth/2);

			_back.x = - Math.round(cwidth/2);
			_back.y = - Math.round(cheight/2);
			_back.z = - Math.round(cdepth/2);

			_left.x = - Math.round(cwidth/2);
			_left.y = - Math.round(cheight/2);
			_left.z = + Math.round(cdepth/2);
			_left.rotationY = 90;

			_right.x = + Math.round(cwidth/2);
			_right.y = - Math.round(cheight/2);
			_right.z = - Math.round(cdepth/2);
			_right.rotationY = -90;

			_top.x = - Math.round(cwidth/2);
			_top.y = + Math.round(cheight/2);
			_top.z = + Math.round(cdepth/2);
			_top.rotationX = -90;

			_bottom.x = - Math.round(cwidth/2);
			_bottom.y = - Math.round(cheight/2);
			_bottom.z = - Math.round(cdepth/2);
			_bottom.rotationX = 90;
			
			addChild(_front);
//			addChild(_back);
//			addChild(_bottom);
//			addChild(_top);
//			addChild(_left);
//			addChild(_right);
			
			for (var i:int=0;i<numChildren;i++){
				_startRot.push({x:getChildAt(i).rotationX,y:getChildAt(i).rotationY,z:getChildAt(i).rotationZ});
			}
			
		}
		
		public function getMaterialsList():MaterialsList{
			return _materials;
		}
		
		override public function set rotationX(value : Number) : void{
			for (var i:int=0;i<numChildren;i++){
				getChildAt(i).rotationX=_startRot[i].x+value;
			}
		}
		override public function set rotationY(value : Number) : void{
			for (var i:int=0;i<numChildren;i++){
				getChildAt(i).rotationY=_startRot[i].y+value;
			}		
		}
		override public function set rotationZ(value : Number) : void{
			for (var i:int=0;i<numChildren;i++){
				getChildAt(i).rotationZ=_startRot[i].z+value;
			}		
		}
		
	}
}
